﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;

namespace Project01_DBSecurity
{
	public partial class Form_Profile : Form
	{
		private int type;
		public Form_Profile(int type, string prof_name)
		{
			InitializeComponent();
			this.type = type;
			switch (type)
			{
				case 1: // tạo mới
					{
						comboBox1.SelectedIndex = 0;
						comboBox2.SelectedIndex = 0;
						comboBox3.SelectedIndex = 0;
						comboBox4.SelectedIndex = 0;
						comboBox5.SelectedIndex = 0;
						comboBox6.SelectedIndex = 0;
						comboBox7.SelectedIndex = 0;
						comboBox8.SelectedIndex = 0;
						comboBox9.SelectedIndex = 0;
						comboBox10.SelectedIndex = 0;
						comboBox11.SelectedIndex = 0;
						comboBox12.SelectedIndex = 0;
						comboBox13.SelectedIndex = 0;
						comboBox14.SelectedIndex = 0;
						comboBox15.SelectedIndex = 0;
						comboBox16.SelectedIndex = 0;
					}
					break;
				case 2: // cập nhật
					{
						textBox1.Text = prof_name;
						textBox1.Enabled = false;
						OracleCommand comm = new OracleCommand();
						comm.Connection = orcl.conn;
						comm.CommandText = "select * from sys.dba_profiles where profile = '" + prof_name + "'";
						try
						{
							OracleDataReader reader = comm.ExecuteReader();
							while (reader.Read())
							{
								string att = reader["RESOURCE_NAME"].ToString();
								string value = reader["LIMIT"].ToString();
								if ( att == "CPU_PER_SESSION" )
									comboBox1.Text = value;
								else if (att == "CPU_PER_CALL")
									comboBox2.Text = value;
								else if (att == "CONNECT_TIME")
									comboBox3.Text = value;
								else if (att == "IDLE_TIME")
									comboBox4.Text = value;
								else if (att == "SESSIONS_PER_USER")
									comboBox5.Text = value;
								else if (att == "LOGICAL_READS_PER_SESSION")
									comboBox6.Text = value;
								else if (att == "LOGICAL_READS_PER_CALL")
									comboBox7.Text = value;
								else if (att == "PRIVATE_SGA")
									comboBox8.Text = value;
								else if (att == "COMPOSITE_LIMIT")
									comboBox9.Text = value;
								else if (att == "PASSWORD_LIFE_TIME")
									comboBox10.Text = value;
								else if (att == "PASSWORD_GRACE_TIME")
									comboBox11.Text = value;
								else if (att == "PASSWORD_REUSE_MAX")
									comboBox12.Text = value;
								else if (att == "PASSWORD_REUSE_TIME")
									comboBox13.Text = value;
								else if (att == "FAILED_LOGIN_ATTEMPTS")
									comboBox14.Text = value;
								else if (att == "PASSWORD_LOCK_TIME")
									comboBox15.Text = value;
								else if (att == "PASSWORD_VERIFY_FUNCTION")
									comboBox16.Text = value;
							}
							reader.Close();
							reader.Dispose();
							comm.Dispose();
						}
						catch (Exception ex)
						{
							MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
						
					}
					break;
				case 3: // xem chi tiết profile
					{
						textBox1.Text = prof_name;
						textBox1.Enabled = false;
						btCancel.Text = "Close";
						btOK.Visible = false;
						comboBox1.Enabled = false;
						comboBox2.Enabled = false;
						comboBox3.Enabled = false;
						comboBox4.Enabled = false;
						comboBox5.Enabled = false;
						comboBox6.Enabled = false;
						comboBox7.Enabled = false;
						comboBox8.Enabled = false;
						comboBox9.Enabled = false;
						comboBox10.Enabled = false;
						comboBox11.Enabled = false;
						comboBox12.Enabled = false;
						comboBox13.Enabled = false;
						comboBox14.Enabled = false;
						comboBox15.Enabled = false;
						comboBox16.Enabled = false;
						OracleCommand comm = new OracleCommand();
						comm.Connection = orcl.conn;
						comm.CommandText = "select * from sys.dba_profiles where profile = '" + prof_name + "'";
						try
						{
							OracleDataReader reader = comm.ExecuteReader();
							while (reader.Read())
							{
								string att = reader["RESOURCE_NAME"].ToString();
								string value = reader["LIMIT"].ToString();
								if (att == "CPU_PER_SESSION")
									comboBox1.Text = value;
								else if (att == "CPU_PER_CALL")
									comboBox2.Text = value;
								else if (att == "CONNECT_TIME")
									comboBox3.Text = value;
								else if (att == "IDLE_TIME")
									comboBox4.Text = value;
								else if (att == "SESSIONS_PER_USER")
									comboBox5.Text = value;
								else if (att == "LOGICAL_READS_PER_SESSION")
									comboBox6.Text = value;
								else if (att == "LOGICAL_READS_PER_CALL")
									comboBox7.Text = value;
								else if (att == "PRIVATE_SGA")
									comboBox8.Text = value;
								else if (att == "COMPOSITE_LIMIT")
									comboBox9.Text = value;
								else if (att == "PASSWORD_LIFE_TIME")
									comboBox10.Text = value;
								else if (att == "PASSWORD_GRACE_TIME")
									comboBox11.Text = value;
								else if (att == "PASSWORD_REUSE_MAX")
									comboBox12.Text = value;
								else if (att == "PASSWORD_REUSE_TIME")
									comboBox13.Text = value;
								else if (att == "FAILED_LOGIN_ATTEMPTS")
									comboBox14.Text = value;
								else if (att == "PASSWORD_LOCK_TIME")
									comboBox15.Text = value;
								else if (att == "PASSWORD_VERIFY_FUNCTION")
									comboBox16.Text = value;
							}
							reader.Close();
							reader.Dispose();
							comm.Dispose();
						}
						catch (Exception ex)
						{
							MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
					}
					break;
				case 4: // xem limits của current user
					{
						textBox1.Enabled = false;
						btCancel.Text = "Close";
						btOK.Visible = false;
						comboBox1.Enabled = false;
						comboBox2.Enabled = false;
						comboBox3.Enabled = false;
						comboBox4.Enabled = false;
						comboBox5.Enabled = false;
						comboBox6.Enabled = false;
						comboBox7.Enabled = false;
						comboBox8.Enabled = false;
						comboBox9.Enabled = false;
						comboBox10.Enabled = false;
						comboBox11.Enabled = false;
						comboBox12.Enabled = false;
						comboBox13.Enabled = false;
						comboBox14.Enabled = false;
						comboBox15.Enabled = false;
						comboBox16.Enabled = false;
						OracleCommand comm = new OracleCommand();
						comm.Connection = orcl.conn;
						comm.CommandText = "select * from USER_PASSWORD_LIMITS";
						try
						{
							OracleDataReader reader = comm.ExecuteReader();
							while (reader.Read())
							{
								string att = reader["RESOURCE_NAME"].ToString();
								string value = reader["LIMIT"].ToString();
								if (att == "CPU_PER_SESSION")
									comboBox1.Text = value;
								else if (att == "CPU_PER_CALL")
									comboBox2.Text = value;
								else if (att == "CONNECT_TIME")
									comboBox3.Text = value;
								else if (att == "IDLE_TIME")
									comboBox4.Text = value;
								else if (att == "SESSIONS_PER_USER")
									comboBox5.Text = value;
								else if (att == "LOGICAL_READS_PER_SESSION")
									comboBox6.Text = value;
								else if (att == "LOGICAL_READS_PER_CALL")
									comboBox7.Text = value;
								else if (att == "PRIVATE_SGA")
									comboBox8.Text = value;
								else if (att == "COMPOSITE_LIMIT")
									comboBox9.Text = value;
								else if (att == "PASSWORD_LIFE_TIME")
									comboBox10.Text = value;
								else if (att == "PASSWORD_GRACE_TIME")
									comboBox11.Text = value;
								else if (att == "PASSWORD_REUSE_MAX")
									comboBox12.Text = value;
								else if (att == "PASSWORD_REUSE_TIME")
									comboBox13.Text = value;
								else if (att == "FAILED_LOGIN_ATTEMPTS")
									comboBox14.Text = value;
								else if (att == "PASSWORD_LOCK_TIME")
									comboBox15.Text = value;
								else if (att == "PASSWORD_VERIFY_FUNCTION")
									comboBox16.Text = value;
							}
							reader.Close();
							reader.Dispose();
							comm.Dispose();
						}
						catch (Exception ex)
						{
							MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
						comm.CommandText = "select * from USER_RESOURCE_LIMITS";
						try
						{
							OracleDataReader reader = comm.ExecuteReader();
							while (reader.Read())
							{
								string att = reader["RESOURCE_NAME"].ToString();
								string value = reader["LIMIT"].ToString();
								if (att == "CPU_PER_SESSION")
									comboBox1.Text = value;
								else if (att == "CPU_PER_CALL")
									comboBox2.Text = value;
								else if (att == "CONNECT_TIME")
									comboBox3.Text = value;
								else if (att == "IDLE_TIME")
									comboBox4.Text = value;
								else if (att == "SESSIONS_PER_USER")
									comboBox5.Text = value;
								else if (att == "LOGICAL_READS_PER_SESSION")
									comboBox6.Text = value;
								else if (att == "LOGICAL_READS_PER_CALL")
									comboBox7.Text = value;
								else if (att == "PRIVATE_SGA")
									comboBox8.Text = value;
								else if (att == "COMPOSITE_LIMIT")
									comboBox9.Text = value;
								else if (att == "PASSWORD_LIFE_TIME")
									comboBox10.Text = value;
								else if (att == "PASSWORD_GRACE_TIME")
									comboBox11.Text = value;
								else if (att == "PASSWORD_REUSE_MAX")
									comboBox12.Text = value;
								else if (att == "PASSWORD_REUSE_TIME")
									comboBox13.Text = value;
								else if (att == "FAILED_LOGIN_ATTEMPTS")
									comboBox14.Text = value;
								else if (att == "PASSWORD_LOCK_TIME")
									comboBox15.Text = value;
								else if (att == "PASSWORD_VERIFY_FUNCTION")
									comboBox16.Text = value;
							}
							reader.Close();
							reader.Dispose();
							comm.Dispose();
						}
						catch (Exception ex)
						{
							MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
					}
					break;
			}
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btOK_Click(object sender, EventArgs e)
		{
			OracleCommand comm = new OracleCommand();
			comm.Connection = orcl.conn;
			if ( type == 1 )
				comm.CommandText = "create profile \"";
			else if ( type == 2 )
				comm.CommandText = "alter profile \"";
			comm.CommandText += textBox1.Text + "\" limit " +
			" CPU_PER_SESSION " + comboBox1.Text +
			" CPU_PER_CALL " + comboBox2.Text +
			" CONNECT_TIME " + comboBox3.Text +
			" IDLE_TIME " + comboBox4.Text +
			" SESSIONS_PER_USER " + comboBox5.Text +
			" LOGICAL_READS_PER_SESSION " + comboBox6.Text +
			" LOGICAL_READS_PER_CALL " + comboBox7.Text +
			" PRIVATE_SGA " + comboBox8.Text +
			" COMPOSITE_LIMIT " + comboBox9.Text +
			" PASSWORD_LIFE_TIME " + comboBox10.Text +
			" PASSWORD_GRACE_TIME " + comboBox11.Text +
			" PASSWORD_REUSE_MAX " + comboBox12.Text +
			" PASSWORD_REUSE_TIME " + comboBox13.Text +
			" FAILED_LOGIN_ATTEMPTS " + comboBox14.Text +
			" PASSWORD_LOCK_TIME " + comboBox15.Text +
			" PASSWORD_VERIFY_FUNCTION " + comboBox16.Text;
			try
			{
				comm.ExecuteNonQuery();
				comm.Dispose();
				MessageBox.Show("Thêm/Sửa profile thành công", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
				this.Close();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
	}
}
